(function() {
    'use strict';
    angular
        .module('milkbeeApp')
        .factory('Category', Category)
        .factory('TreeCategory', TreeCategory)
        .factory('SubCategory', SubCategory);

    Category.$inject = ['$resource', 'DateUtils'];
    SubCategory.$inject = ['$resource', 'DateUtils'];
    TreeCategory.$inject = ['$resource', 'DateUtils'];
    function Category ($resource, DateUtils) {
        var resourceUrl =  'api/categories/:id';

        return $resource(resourceUrl, {}, {
            'query': { method: 'GET', isArray: true},
            'get': {
                method: 'GET',
                transformResponse: function (data) {
                    if (data) {
                        data = angular.fromJson(data);
                        data.createdAt = DateUtils.convertDateTimeFromServer(data.createdAt);
                    }
                    return data;
                }
            },
            'update': { method:'PUT' }
        });
    }
        function SubCategory ($resource, DateUtils) {
        	var resourceUrl =  'api/subcategories/:parentId';
        	
        	return $resource(resourceUrl, {}, {
        		'query': { method: 'GET', isArray: true}
        	});
    }
        function TreeCategory ($resource, DateUtils) {
        	var resourceUrl =  'api/treecategories';
        	
        	return $resource(resourceUrl, {}, {
        		'query': { method: 'GET', isArray: true}
        	});
        }
})();
